﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Data;

namespace AnonsaiWPF
{
    public class ClipNameToIdConverter : IValueConverter
    {


        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return 0;

            //if (value == null)
            //    return -1;

            //for (int i = 0; i < productNames.Length / 2; i++)
            //    if (productNames[i, 0] == value.ToString())
            //        return i;

            //return -1;
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            return "aaa";//listAnnounceRecord. productNames[(int)value, 0];
        }

        #endregion
    }

    public class DateTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                long ticks = long.Parse(value.ToString());
                TimeSpan tm = new TimeSpan(ticks);
                return string.Format("{0:c}", tm);
            }
            //return "10";
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return DateTime.Parse(value.ToString());
            }
            else
            {
                return DateTime.Now.Date;
            }
        }
    }


    public class AnonsIDtoClipDurationConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                if (value != "0")
                {
                    DataTable tb = Functionality.dbc.getTable("select ac.Duration from Anonsai an left join AnonsaiClips ac on an.ClipId=ac.id where an.id=" + value.ToString());
                    if (tb.Rows.Count > 0)
                    {
                        return tb.Rows[0][0].ToString();
                    }
                    else return "";
                }
                else return "";
            }
            //return "10";
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return value.ToString();
            }
            else
            {
                return "";
            }
        }
    }

    public class AnonsIDtoClipNameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                if (value != "0")
                {
                    DataTable tb = Functionality.dbc.getTable("select ac.ClipName from Anonsai an left join AnonsaiClips ac on an.ClipId=ac.id where an.id=" + value.ToString());
                    if (tb.Rows.Count > 0)
                    {
                        return tb.Rows[0][0].ToString();
                    }
                    else return "";
                }
                else return "";
            }
            //return "10";
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return value.ToString();
            }
            else
            {
                return "";
            }
        }
    }

    public class AnonsIDConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                if (value != "0")
                {
                    DataTable tb = Functionality.dbc.getTable("select Name from Anonsai where id=" + value.ToString());
                    if (tb.Rows.Count > 0)
                    {
                        return tb.Rows[0][0].ToString();
                    }
                    else return "";
                }
                else return "";
            }
            //return "10";
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return value.ToString();
            }
            else
            {
                return "";
            }
        }
    }


    public class BlockPosConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                if (value.ToString() != "-1")
                {
                    return value;
                }
                else return "";
            }
            //return "10";
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return value.ToString();
            }
            else
            {
                return "";
            }
        }
    }

    public class RaitConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return "0%";
            }
            else
            {
                return value+"%";
            }
            //return "10";
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return value.ToString().Replace("%","");
            }
            else
            {
                return "";
            }
        }
    }

    public class StartTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return "0";
            }
            else
            {
                return value ;
            }
            //return "10";
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return value.ToString();
            }
            else
            {
                return "";
            }
        }
    }
}
